setting cookie doesn"t work

setting cookie doesn"t work

am 03.08.2009 17:35:20 von Grega Leskovsek

Here is one of my final objectives at PHP introductory course ot
O'Reilly. The strange thing that setting a cookie in that program
les13uadownload.php doesn't work. I don't have a clue - sometimes in
other programs when I run them they set cookies and sometimes not. To
see the full functionality of this "program" I had to set cookie
sevendays in another program. Please check where is my bug. Thanks in
advance, Grega from Slovenia

if (!empty($_GET['delete_cookie'])) {
setcookie("sevendays", "", time()-3600); //delete the cookie to
allow to download the file more than 1x in 7days as another user
}

if (isset($_POST['check'])AND(isset($_POST['email']))AND(empty( $_COOKIE["sevendays"])))
{#user clicked form download button
$email = $_POST['email'];
setcookie("sevendays", "email", time()+60*60*24*7);
$filepath = $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure.pdf";
if (file_exists($filepath)) {
header("Content-Type: application/force-download");
header("Content-Disposition:filename=\"brochure.pdf\"");
$fd = fopen($filepath,'rb');
fpassthru($fd);
fclose($fd);
}
}#isset(check)
?>

require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
$ua = $_SERVER['HTTP_USER_AGENT']."
";
if (preg_match("/Macintosh/",$ua)) {
if (!preg_match("/Firefox/",$ua)) {
echo "For Macintosh You need to use Firefox! href=\"http://www.apple.com/downloads/macosx/internet_utilit ies/mozillafirefox.html\">Download
here ...";
exit();
}
}
if (preg_match("/Windows/",$ua)) {
if (!preg_match("/MSIE/",$ua)) {
echo "For Windows You need to use Internet Explorer!";
exit();
}
}
$ip = $_SERVER['REMOTE_ADDR'];
$ipCheck = substr($ip,0,3);
if ($ipCheck == "202") {echo "I don't trust You."; exit();}
?>
if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
".$_COOKIE["sevendays"]." DAYS!!!";
if ($_COOKIE["sevendays"]) {
echo $_COOKIE["sevendays"];
?>
Not my email: $_COOKIE["sevendays"]; ?>?
}else {
?>


Enter Your e-mail: size="25" name="email" value="">
/>

"no"; ?>"/>

}
require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
print_r($_COOKIE);
echo "XX".$_COOKIE["sevendays"]."XX";
?>


--
When the sun rises I receive and when it sets I forgive ->
http://users.skavt.net/~gleskovs/
All the Love, Grega Leskov'sek

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: setting cookie doesn"t work

am 03.08.2009 17:38:47 von Ashley Sheridan

On Mon, 2009-08-03 at 17:35 +0200, Grega Leskovsek wrote:
> Here is one of my final objectives at PHP introductory course ot
> O'Reilly. The strange thing that setting a cookie in that program
> les13uadownload.php doesn't work. I don't have a clue - sometimes in
> other programs when I run them they set cookies and sometimes not. To
> see the full functionality of this "program" I had to set cookie
> sevendays in another program. Please check where is my bug. Thanks in
> advance, Grega from Slovenia
>
> > if (!empty($_GET['delete_cookie'])) {
> setcookie("sevendays", "", time()-3600); //delete the cookie to
> allow to download the file more than 1x in 7days as another user
> }
>
> if (isset($_POST['check'])AND(isset($_POST['email']))AND(empty( $_COOKIE["sevendays"])))
> {#user clicked form download button
> $email = $_POST['email'];
> setcookie("sevendays", "email", time()+60*60*24*7);
> $filepath = $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure.pdf";
> if (file_exists($filepath)) {
> header("Content-Type: application/force-download");
> header("Content-Disposition:filename=\"brochure.pdf\"");
> $fd = fopen($filepath,'rb');
> fpassthru($fd);
> fclose($fd);
> }
> }#isset(check)
> ?>
>
> > require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
> $ua = $_SERVER['HTTP_USER_AGENT']."
";
> if (preg_match("/Macintosh/",$ua)) {
> if (!preg_match("/Firefox/",$ua)) {
> echo "For Macintosh You need to use Firefox! > href=\"http://www.apple.com/downloads/macosx/internet_utilit ies/mozillafirefox.html\">Download
> here ...";
> exit();
> }
> }
> if (preg_match("/Windows/",$ua)) {
> if (!preg_match("/MSIE/",$ua)) {
> echo "For Windows You need to use Internet Explorer!";
> exit();
> }
> }
> $ip = $_SERVER['REMOTE_ADDR'];
> $ipCheck = substr($ip,0,3);
> if ($ipCheck == "202") {echo "I don't trust You."; exit();}
> ?>
> > if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
> ".$_COOKIE["sevendays"]." DAYS!!!";
> if ($_COOKIE["sevendays"]) {
> echo $_COOKIE["sevendays"];
> ?>
> Not my email: > $_COOKIE["sevendays"]; ?>?
> > }else {
> ?>
>


> Enter Your e-mail: > size="25" name="email" value="">
> > />
>
> > "no"; ?>"/>
>

> > }
> require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
> print_r($_COOKIE);
> echo "XX".$_COOKIE["sevendays"]."XX";
> ?>
>
>
> --
> When the sun rises I receive and when it sets I forgive ->
> http://users.skavt.net/~gleskovs/
> All the Love, Grega Leskov'sek
>
Just a quick thought. You're not trying to read in the value of the
cookie in the same page as you are setting it are you? You can do this
with session variables, but not cookies, as PHP only reads them if they
are sent in with the page request headers. So if you set a cookie, it
won't be visible to your script until the page is refreshed, which
forces the browser to resend the cookies, including the newly set cookie
value.


Thanks,
Ash
http://www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: setting cookie doesn"t work

am 03.08.2009 17:39:46 von Grega Leskovsek

And here is the objective:
Alter your Acme, Inc. download interface from the previous lesson(I
had to not to allow users on computers straing IP with 202 and allow
only IE for Win and FF for Mac to download file) so that each user
must register her email address in order to download the file. Use
cookies to detect whether the user has already registered, and to
ensure that the user downloads the file only once within 7 days of
registering.

As always, validate the forms, comment your code, and explain your results!


2009/8/3 Grega Leskovsek :
> Here is one of my final objectives at PHP introductory course ot
> O'Reilly. The strange thing that setting a cookie in that program
> les13uadownload.php doesn't work. I don't have a clue - sometimes in
> other programs when I run them they set cookies and sometimes not. To
> see the full functionality of this "program" I had to set cookie
> sevendays in another program. Please check where is my bug. Thanks in
> advance, Grega from Slovenia
>
> > if (!empty($_GET['delete_cookie'])) {
>   setcookie("sevendays", "", time()-3600); //delete the cookie to
> allow to download the file more than 1x in 7days as another user
> }
>
> if (isset($_POST['check'])AND(isset($_POST['email']))AND(empty( $_COOKIE["=
sevendays"])))
> {#user clicked form download button
>  $email =3D $_POST['email'];
>  setcookie("sevendays", "email", time()+60*60*24*7);
>  $filepath =3D $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure.=
pdf";
>  if (file_exists($filepath)) {
>     header("Content-Type: application/force-download");
>     header("Content-Disposition:filename=3D\"brochure.pdf\"");
>     $fd =3D fopen($filepath,'rb');
>     fpassthru($fd);
>     fclose($fd);
>  }
> }#isset(check)
> ?>
>
> //You dont need this line>
require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
> $ua =3D $_SERVER['HTTP_USER_AGENT']."
";
> if (preg_match("/Macintosh/",$ua)) {
>  if (!preg_match("/Firefox/",$ua)) {
>    echo "For Macintosh You need to use Firefox! > href=3D\"http://www.apple.com/downloads/macosx/internet_util ities/mozilla=
firefox.html\">Download
> here ...";
>    exit();
>  }
> }
> if (preg_match("/Windows/",$ua)) {
>  if (!preg_match("/MSIE/",$ua)) {
>    echo "For Windows You need to use Internet Explorer!";
>    exit();
>  }
> }
> $ip =3D $_SERVER['REMOTE_ADDR'];
> $ipCheck =3D substr($ip,0,3);
> if ($ipCheck == "202") {echo "I don't trust You."; exit();}
> ?>
> > if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
> ".$_COOKIE["sevendays"]." DAYS!!!";
> if ($_COOKIE["sevendays"]) {
>  echo $_COOKIE["sevendays"];
>  ?>
>  Not my email: echo
> $_COOKIE["sevendays"]; ?>?

>   > }else {
>  ?>
>  

d.php">
>  Enter Your e-mail: t"
> size=3D"25" name=3D"email" value=3D"">
>   ick=3D"if
> (isFormValid(document.vnosnaForma)) {document.vnosnaForma.submit();}"
> />
>  
>   > (!empty($_COOKIE["sevendays"])) echo $_COOKIE["sevendays"]; else echo
> "no"; ?>"/>
>  

>   > }
//You dont need this line>
require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
> print_r($_COOKIE);
> echo "XX".$_COOKIE["sevendays"]."XX";
> ?>
>
>
> --
> When the sun rises I receive and when it sets I forgive ->
> http://users.skavt.net/~gleskovs/
> All the Love, Grega Leskov'sek
>



--=20
When the sun rises I receive and when it sets I forgive ->
http://users.skavt.net/~gleskovs/
All the Love, Grega Leskov'sek

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: setting cookie doesn"t work

am 03.08.2009 20:44:00 von Grega Leskovsek

I made two files now of the program: (the main one and the one where I
set the cookie and open pdf)and I noticed that it doesn't work setting
the cookie the first time I call the file where I set the cookie but
only after the second header redirection.
How do I refresh the page in php?

Thanks in advance, Grega from Slovenia

2009/8/3 Grega Leskovsek :
> And here is the objective:
> Alter your Acme, Inc. download interface from the previous lesson(I
> had to not to allow users on computers straing IP with 202 and allow
> only IE for Win and FF for Mac to download file) so that each user
> must register her email address in order to download the file. Use
> cookies to detect whether the user has already registered, and to
> ensure that the user downloads the file only once within 7 days of
> registering.
>
> As always, validate the forms, comment your code, and explain your result=
s!
>
>
> 2009/8/3 Grega Leskovsek :
>> Here is one of my final objectives at PHP introductory course ot
>> O'Reilly. The strange thing that setting a cookie in that program
>> les13uadownload.php doesn't work. I don't have a clue - sometimes in
>> other programs when I run them they set cookies and sometimes not. To
>> see the full functionality of this "program" I had to set cookie
>> sevendays in another program. Please check where is my bug. Thanks in
>> advance, Grega from Slovenia
>>
>> >> if (!empty($_GET['delete_cookie'])) {
>>   setcookie("sevendays", "", time()-3600); //delete the cookie to
>> allow to download the file more than 1x in 7days as another user
>> }
>>
>> if (isset($_POST['check'])AND(isset($_POST['email']))AND(empty( $_COOKIE[=
"sevendays"])))
>> {#user clicked form download button
>>  $email =3D $_POST['email'];
>>  setcookie("sevendays", "email", time()+60*60*24*7);
>>  $filepath =3D $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure=
..pdf";
>>  if (file_exists($filepath)) {
>>     header("Content-Type: application/force-download");
>>     header("Content-Disposition:filename=3D\"brochure.pdf\"");
>>     $fd =3D fopen($filepath,'rb');
>>     fpassthru($fd);
>>     fclose($fd);
>>  }
>> }#isset(check)
>> ?>
>>
>> > //You dont need this line>
> require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
>> $ua =3D $_SERVER['HTTP_USER_AGENT']."
";
>> if (preg_match("/Macintosh/",$ua)) {
>>  if (!preg_match("/Firefox/",$ua)) {
>>    echo "For Macintosh You need to use Firefox! >> href=3D\"http://www.apple.com/downloads/macosx/internet_util ities/mozill=
afirefox.html\">Download
>> here ...";
>>    exit();
>>  }
>> }
>> if (preg_match("/Windows/",$ua)) {
>>  if (!preg_match("/MSIE/",$ua)) {
>>    echo "For Windows You need to use Internet Explorer!";
>>    exit();
>>  }
>> }
>> $ip =3D $_SERVER['REMOTE_ADDR'];
>> $ipCheck =3D substr($ip,0,3);
>> if ($ipCheck == "202") {echo "I don't trust You."; exit();}
>> ?>
>> >> if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
>> ".$_COOKIE["sevendays"]." DAYS!!!";
>> if ($_COOKIE["sevendays"]) {
>>  echo $_COOKIE["sevendays"];
>>  ?>
>>  Not my email: echo
>> $_COOKIE["sevendays"]; ?>?

>>   >> }else {
>>  ?>
>>  

ad.php">
>>  Enter Your e-mail: xt"
>> size=3D"25" name=3D"email" value=3D"">
>>   lick=3D"if
>> (isFormValid(document.vnosnaForma)) {document.vnosnaForma.submit();}"
>> />
>>  
>>   >> (!empty($_COOKIE["sevendays"])) echo $_COOKIE["sevendays"]; else echo
>> "no"; ?>"/>
>>  

>>   >> }
> //You dont need this line>
> require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
>> print_r($_COOKIE);
>> echo "XX".$_COOKIE["sevendays"]."XX";
>> ?>
>>
>>
>> --
>> When the sun rises I receive and when it sets I forgive ->
>> http://users.skavt.net/~gleskovs/
>> All the Love, Grega Leskov'sek
>>
>
>
>
> --
> When the sun rises I receive and when it sets I forgive ->
> http://users.skavt.net/~gleskovs/
> All the Love, Grega Leskov'sek
>



--=20
When the sun rises I receive and when it sets I forgive ->
http://users.skavt.net/~gleskovs/
All the Love, Grega Leskov'sek

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: setting cookie doesn"t work

am 03.08.2009 21:27:23 von Ashley Sheridan

On Mon, 2009-08-03 at 20:44 +0200, Grega Leskovsek wrote:
> I made two files now of the program: (the main one and the one where I
> set the cookie and open pdf)and I noticed that it doesn't work setting
> the cookie the first time I call the file where I set the cookie but
> only after the second header redirection.
> How do I refresh the page in php?
>
> Thanks in advance, Grega from Slovenia
>
> 2009/8/3 Grega Leskovsek :
> > And here is the objective:
> > Alter your Acme, Inc. download interface from the previous lesson(I
> > had to not to allow users on computers straing IP with 202 and allow
> > only IE for Win and FF for Mac to download file) so that each user
> > must register her email address in order to download the file. Use
> > cookies to detect whether the user has already registered, and to
> > ensure that the user downloads the file only once within 7 days of
> > registering.
> >
> > As always, validate the forms, comment your code, and explain your results!
> >
> >
> > 2009/8/3 Grega Leskovsek :
> >> Here is one of my final objectives at PHP introductory course ot
> >> O'Reilly. The strange thing that setting a cookie in that program
> >> les13uadownload.php doesn't work. I don't have a clue - sometimes in
> >> other programs when I run them they set cookies and sometimes not. To
> >> see the full functionality of this "program" I had to set cookie
> >> sevendays in another program. Please check where is my bug. Thanks in
> >> advance, Grega from Slovenia
> >>
> >> > >> if (!empty($_GET['delete_cookie'])) {
> >> setcookie("sevendays", "", time()-3600); //delete the cookie to
> >> allow to download the file more than 1x in 7days as another user
> >> }
> >>
> >> if (isset($_POST['check'])AND(isset($_POST['email']))AND(empty( $_COOKIE["sevendays"])))
> >> {#user clicked form download button
> >> $email = $_POST['email'];
> >> setcookie("sevendays", "email", time()+60*60*24*7);
> >> $filepath = $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure.pdf";
> >> if (file_exists($filepath)) {
> >> header("Content-Type: application/force-download");
> >> header("Content-Disposition:filename=\"brochure.pdf\"");
> >> $fd = fopen($filepath,'rb');
> >> fpassthru($fd);
> >> fclose($fd);
> >> }
> >> }#isset(check)
> >> ?>
> >>
> >> > > //You dont need this line>
> > require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
> >> $ua = $_SERVER['HTTP_USER_AGENT']."
";
> >> if (preg_match("/Macintosh/",$ua)) {
> >> if (!preg_match("/Firefox/",$ua)) {
> >> echo "For Macintosh You need to use Firefox! > >> href=\"http://www.apple.com/downloads/macosx/internet_utilit ies/mozillafirefox.html\">Download
> >> here ...";
> >> exit();
> >> }
> >> }
> >> if (preg_match("/Windows/",$ua)) {
> >> if (!preg_match("/MSIE/",$ua)) {
> >> echo "For Windows You need to use Internet Explorer!";
> >> exit();
> >> }
> >> }
> >> $ip = $_SERVER['REMOTE_ADDR'];
> >> $ipCheck = substr($ip,0,3);
> >> if ($ipCheck == "202") {echo "I don't trust You."; exit();}
> >> ?>
> >> > >> if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
> >> ".$_COOKIE["sevendays"]." DAYS!!!";
> >> if ($_COOKIE["sevendays"]) {
> >> echo $_COOKIE["sevendays"];
> >> ?>
> >> Not my email: > >> $_COOKIE["sevendays"]; ?>?
> >> > >> }else {
> >> ?>
> >>


> >> Enter Your e-mail: > >> size="25" name="email" value="">
> >> > >> />
> >>
> >> > >> "no"; ?>"/>
> >>

> >> > >> }
> > //You dont need this line>
> > require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
> >> print_r($_COOKIE);
> >> echo "XX".$_COOKIE["sevendays"]."XX";
> >> ?>
> >>
> >>
> >> --
> >> When the sun rises I receive and when it sets I forgive ->
> >> http://users.skavt.net/~gleskovs/
> >> All the Love, Grega Leskov'sek
> >>
> >
> >
> >
> > --
> > When the sun rises I receive and when it sets I forgive ->
> > http://users.skavt.net/~gleskovs/
> > All the Love, Grega Leskov'sek
> >
>
>
>
> --
> When the sun rises I receive and when it sets I forgive ->
> http://users.skavt.net/~gleskovs/
> All the Love, Grega Leskov'sek
>
You could send a header() refresh, write some javascript to the page, or
use a meta tag to refresh. Or, you could set the variable in the session
as well, and then you can use code to determine whichever one exists and
use that.


Thanks,
Ash
http://www.ashleysheridan.co.uk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php